<!--
 * @Descripttion: 用户手册管理
 * @version: 
 * @Author: 穆宏宇
 * @Date: 2020-10-21 13:33:50
 * @LastEditors: 穆宏宇
 * @LastEditTime: 2020-11-04 17:48:40
-->
<template>
	<div class="bigDiv">
		<el-form ref="queryForm" :inline="true" label-width="88px">
			<div class="topNav">
				<el-button @click="exportZip" type="text">下载zip</el-button>
				<el-button @click="downloadTemplate" type="text">下载模板</el-button>
				<el-button @click="handleExport" type="text">批量导出</el-button>
				<el-button @click="handleDeal" type="text">批量已处理</el-button>
				<el-button @click="handleDeal2" type="text">批量未处理</el-button>
				<el-button type="text">
					<el-upload :action="fileUrl" :headers="fileHeaders" :show-file-list="false" :file-list="fileList"
						:on-success="handleAvatarSuccess" :on-error="handleAvatarError" multiple :limit="10000">
						<div class="el-upload__text">导入excel</div>
					</el-upload>
				</el-button>
				<el-button @click="reset" type="text" style="float: right;">重置</el-button>
				<el-button @click="getData()" type="text" style="float: right;">搜索</el-button>
			</div>
			<el-form-item label="批次号">
				<el-input type="text" v-model="queryParams.batchNumber" clearable />
			</el-form-item>
			<el-form-item label="订单号">
				<el-input type="text" v-model="queryParams.orderNo" clearable />
			</el-form-item>
			<el-form-item label="收票人姓名">
				<el-input type="text" v-model="queryParams.invoiceTaker" clearable />
			</el-form-item>
			<el-form-item label="是否处理">
				<el-select v-model="queryParams.handle" @change="changeHandle" clearable>
					<el-option label="已处理" value="1"></el-option>
					<el-option label="未处理" value="0"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="有无发票号">
				<el-select v-model="queryParams.invoiceConfirm" @change="changeInvoiceConfirm" clearable>
					<el-option label="全部" value=""></el-option>
					<el-option label="有" value="1"></el-option>
					<el-option label="无" value="0"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="时间">
				<!-- <el-date-picker clearable v-model="queryParams.createTime" type="date" value-format="yyyy-MM-dd"
					placeholder="选择结束时间">
				</el-date-picker> -->
				<el-select v-model="queryParams.createTime" placeholder="请选择时间" clearable filterable>
					<el-option v-for="item in timeArr" :label="item" :value="item">
					</el-option>
				</el-select>
			</el-form-item>
		</el-form>

		<el-row class="handbooks-box">
			<el-table :data="list" border style="width: 100%" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" align="center">
				</el-table-column>
				<el-table-column prop="batch_number" label="批次号">
				</el-table-column>
				<el-table-column prop="order_no" label="订单号">
				</el-table-column>
				<el-table-column prop="invoice" label="发票号">
				</el-table-column>
				<el-table-column prop="invoice_taker" label="收票人姓名">
				</el-table-column>
				<el-table-column prop="invoice_taker_phone" label="收票人电话">
				</el-table-column>
				<el-table-column prop="manager_name" label="项目经理姓名">
				</el-table-column>
				<el-table-column prop="handle" label="是否处理">
					<template slot-scope="scope">
					  <div v-if="scope.row.handle == 1">已处理</div>
					  <div v-else>未处理</div>
					</template>
				</el-table-column>
				<el-table-column label="操作">
				  <template slot-scope="scope">
				    <el-button type="primary" size="mini" @click="download(scope.row)">
				      <i class="el-icon-download"></i>
				      导出
				    </el-button>
				  </template>
				</el-table-column>
			</el-table>


			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="currentPage" :page-size="pageSize" layout=" ->, total, sizes, prev, pager, next, jumper"
				:total="total">
			</el-pagination>
			<!-- <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
	  	@pagination="getData" />
	  		<el-pagination
	  		    @size-change="handleSizeChange"
	  		    @current-change="handleCurrentChange"
	  		    :current-page="currentPage"
	  		    :page-size="pageSize"
	  		    layout=" ->, total, sizes, prev, pager, next, jumper"
	  			
	  		    :total="total">
	  		</el-pagination> -->
		</el-row>
	</div>
</template>

<script>
	import fileUpdata from "@/api/file.config";
	import {
		deleteFile
	} from "@/api/common.api"
	import {
		selInvoiceReconciliaion,
		invoiceReconciliation,
		downloadInvoiceZhu,
		exportInvoiceToOrderNo,
		upHandle,
		exportInvoiceToOrderNoBatch,
		downloadZip,
		sqlHandleTime
	} from '@/api/admin.api'
	import {
		getUserInfoByToken
	} from '@/api/mall.api'
	export default {
		data() {
			return {
				fileHeaders: fileUpdata.headers(),
				fileUrl: "/vapi/excel/import/importInvoiceReconciliation",
				fileList: [],
				list: [],
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					batchNumber: '',
					orderNo:'',
					invoiceTaker:'',
					handle:'',
					createTime:'',
					invoiceConfirm:''
				},
				selectStr:"",
				selectStr2:"",
				
				timeArr: [],
				name:''
			};
		},
		mounted() {
			this.getData()
			this.sqlHandleTime();
			this.getUserInfo();
		},
		methods: {
			getUserInfo() {
				var that = this;
				getUserInfoByToken({
					account:localStorage.getItem("account")
				}).then((res)=>{
					debugger
					that.name = res.name;
				})
			},
			sqlHandleTime() {
				var that = this;
				var arr = [];
				sqlHandleTime().then((res) => {
					for (var i = 0; i < res.length; i++) {
						arr.push(res[i].handleDate);
					}
					that.timeArr = arr;
				})
			},
			exportZip() {
				if (this.queryParams.createTime == '') {
					this.$message({
						type: 'error',
						message: '请选择时间!'
					});
					return
				}
				downloadZip({
					name: this.name,
					date: this.queryParams.createTime
				}).then(res => {
					const blob = new Blob([res], {
						type: 'application/zip'
					})
					this.saveAs(blob, '导出.zip')
				})
			},
			handleDeal(){
				upHandle({
					ids:this.selectStr,
					handle:1
				}).then(res=>{
					if(res.code == 200){
						this.$message.success("处理成功");
						this.getData();
					}
				})
			},
			handleDeal2(){
				upHandle({
					ids:this.selectStr,
					handle:0
				}).then(res=>{
					if(res.code == 200){
						this.$message.success("处理成功");
						this.getData();
					}
				})
			},
			handleExport(){
				exportInvoiceToOrderNoBatch({
					orderNo:this.selectStr2
				}).then(res=>{
					const blob = new Blob([res], {
						type: 'application/zip'
					})
					debugger
					this.saveAs(blob, '批量导出.zip')
				})
			},
			changeHandle(e){
				this.handle = e;
			},
			changeInvoiceConfirm(e){
				this.invoiceConfirm = e;
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
			  var arr = [];
			  var arr2 = [];
			  for (var i = 0; i < selection.length; i++) {
			  	arr.push(selection[i].id);
			  	arr2.push(selection[i].order_no);
			  }
			  this.selectStr = arr.join(",");
			  this.selectStr2 = arr2.join(",");
			},
			downloadTemplate(){
				downloadInvoiceZhu({
					name:this.name
				}).then(res=>{
					const blob = new Blob([res], {
						type: 'application/vnd.ms-excel'
					})				
					debugger
					this.saveAs(blob, '模板.xlsx')
				})
			},
			saveAs(blob, filename) {
				if (window.navigator.msSaveOrOpenBlob) {
					navigator.msSaveBlob(blob, filename)
				} else {
					const link = document.createElement('a')
					const body = document.querySelector('body')
					link.href = window.URL.createObjectURL(blob)
					link.download = filename
					link.style.display = 'none'
					body.appendChild(link)
					link.click()
					body.removeChild(link)
					window.URL.revokeObjectURL(link.href)
				}
			},
			reset() {
				this.queryParams = {
					pageNum: 1,
					pageSize: 100000,
					batchNumber: '',
					orderNo:'',
					invoiceTaker:'',
					invoiceConfirm:''
				}
			},
			exportOrder(order_no) {
				invoiceReconciliation({
					order_no: order_no
				}).then(res => {
					this.list = res.rows
					this.total = res.total
				})
			},
			handleSizeChange(val) {
				this.queryParams.pageSize = val
				this.getData()
			},
			handleCurrentChange(val) {
				this.queryParams.pageNum = val
				this.getData()
			},
			// 上传成功
			handleAvatarSuccess(res) {

				if (res && res.code === 200) {
					this.$message({
						message: '上传成功',
						type: 'success'
					})
					this.fileList = []
					this.getData()
				}
			},
			//  上传失败
			handleAvatarError(err) {
				this.$message({
					message: '上传失败',
					type: 'warning'
				})
			},
			// 下载
			download(item) {
				// 单行导出
				exportInvoiceToOrderNo({
					orderNo:item.order_no
				}).then(res=>{
					const blob = new Blob([res], {
						type: 'application/vnd.ms-excel'
					})
					this.saveAs(blob, '对账.xlsx')
				})
				
			},
			// 查询列表
			getData() {
				selInvoiceReconciliaion(this.queryParams).then(res => {
					this.list = res.rows
					this.total = res.total
				})
			},
			// 删除
			deleteItem(item) {
				this.$confirm('确定删除文件', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					deleteFile(10, item.id).then(res => {
						this.$message({
							type: 'success',
							message: '删除成功!'
						});
						this.getData()
					})

				});
			}
		},
	};
</script>

<style scoped lang='scss'>
	.handbooks {
		.title {
			margin: 10px 0;
			// font-weight: bold;
			text-align: left;
		}

		.upload-box {
			margin-top: 30px;
			text-align: center;
		}
	}
</style>
